package org.xtext.ic.dsl.generator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.xbase.lib.BooleanExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.eclipse.xtext.xtend2.lib.StringConcatenation;
import org.xtext.ic.dsl.ICDslExtensions;
import org.xtext.ic.dsl.iCDsl.Attribute;
import org.xtext.ic.dsl.iCDsl.Entity;
import org.xtext.ic.dsl.iCDsl.Model;
import org.xtext.ic.dsl.iCDsl.PackagePrefix;
import org.xtext.ic.dsl.iCDsl.Type;
import org.xtext.ic.dsl.iCDsl.TypeDef;

@SuppressWarnings("all")
public class ModelExtensions extends ICDslExtensions {
  
  public StringConcatenation compile(final Model m, final Entity e) {
    StringConcatenation _builder = new StringConcatenation();
    {
      EObject _eContainer = e.eContainer();
      if ((_eContainer instanceof org.xtext.ic.dsl.iCDsl.Layer)) {
        _builder.append("package ");
        PackagePrefix _package = m.getPackage();
        String _packageName = this.packageName(_package, e);
        _builder.append(_packageName, "");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    String _imports = this.imports(m, e);
    _builder.append(_imports, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    StringConcatenation _classes = this.classes(e);
    _builder.append(_classes, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public String imports(final Model m, final Entity e) {
    String _xblockexpression = null;
    {
      String importList = "";
      boolean pass = false;
      EList<Type> _types = m.getTypes();
      for (final Type t : _types) {
        if ((t instanceof org.xtext.ic.dsl.iCDsl.TypeDef)) {
          {
            TypeDef td = ((TypeDef) t);
            String _mappedType = td.getMappedType();
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(_mappedType);
            boolean _operator_not = BooleanExtensions.operator_not(_isNullOrEmpty);
            if (_operator_not) {
              {
                pass = false;
                EList<Attribute> _attributes = e.getAttributes();
                for (final Attribute a : _attributes) {
                  boolean _operator_and = false;
                  boolean _operator_not_1 = BooleanExtensions.operator_not(pass);
                  if (!_operator_not_1) {
                    _operator_and = false;
                  } else {
                    String _name = td.getName();
                    Type _type = a.getType();
                    String _name_1 = _type.getName();
                    boolean _equals = _name.equals(_name_1);
                    _operator_and = BooleanExtensions.operator_and(_operator_not_1, _equals);
                  }
                  if (_operator_and) {
                    {
                      String _operator_plus = StringExtensions.operator_plus(importList, "import ");
                      String _mappedType_1 = td.getMappedType();
                      String _operator_plus_1 = StringExtensions.operator_plus(_operator_plus, _mappedType_1);
                      String _operator_plus_2 = StringExtensions.operator_plus(_operator_plus_1, ";");
                      String _property = System.getProperty("line.separator");
                      String _operator_plus_3 = StringExtensions.operator_plus(_operator_plus_2, _property);
                      importList = _operator_plus_3;
                      pass = true;
                    }
                  }
                }
              }
            }
          }
        }
      }
      pass = false;
      EList<Attribute> _attributes_1 = e.getAttributes();
      for (final Attribute a_1 : _attributes_1) {
        boolean _operator_and_1 = false;
        boolean _operator_not_2 = BooleanExtensions.operator_not(pass);
        if (!_operator_not_2) {
          _operator_and_1 = false;
        } else {
          boolean _isMany = a_1.isMany();
          _operator_and_1 = BooleanExtensions.operator_and(_operator_not_2, _isMany);
        }
        if (_operator_and_1) {
          {
            String _operator_plus_4 = StringExtensions.operator_plus(importList, "import java.util.List;");
            String _property_1 = System.getProperty("line.separator");
            String _operator_plus_5 = StringExtensions.operator_plus(_operator_plus_4, _property_1);
            importList = _operator_plus_5;
            pass = true;
            return importList;
          }
        }
      }
      _xblockexpression = (importList);
    }
    return _xblockexpression;
  }
  
  public StringConcatenation classes(final Entity e) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("public class ");
    String _name = e.getName();
    _builder.append(_name, "");
    {
      Entity _superEntity = e.getSuperEntity();
      boolean _operator_notEquals = ObjectExtensions.operator_notEquals(_superEntity, null);
      if (_operator_notEquals) {
        _builder.append(" extends ");
        Entity _superEntity_1 = e.getSuperEntity();
        String _name_1 = _superEntity_1.getName();
        _builder.append(_name_1, "");
      }
    }
    _builder.append("{");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    {
      EList<Attribute> _attributes = e.getAttributes();
      for(final Attribute a : _attributes) {
        _builder.append("\t");
        _builder.append("private ");
        StringConcatenation _variable = this.variable(a);
        _builder.append(_variable, "	");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
      }
    }
    {
      EList<Attribute> _attributes_1 = e.getAttributes();
      for(final Attribute a_1 : _attributes_1) {
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public ");
        StringConcatenation _typeDeclaration = this.typeDeclaration(a_1);
        _builder.append(_typeDeclaration, "	");
        _builder.append(" get");
        String _name_2 = a_1.getName();
        String _firstUpper = StringExtensions.toFirstUpper(_name_2);
        _builder.append(_firstUpper, "	");
        _builder.append("(){");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return ");
        String _name_3 = a_1.getName();
        _builder.append(_name_3, "		");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public void set");
        String _name_4 = a_1.getName();
        String _firstUpper_1 = StringExtensions.toFirstUpper(_name_4);
        _builder.append(_firstUpper_1, "	");
        _builder.append("(");
        StringConcatenation _variable_1 = this.variable(a_1);
        _builder.append(_variable_1, "	");
        _builder.append("){");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("this.");
        String _name_5 = a_1.getName();
        _builder.append(_name_5, "		");
        _builder.append(" = ");
        String _name_6 = a_1.getName();
        _builder.append(_name_6, "		");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
      }
    }
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
}